/*
 * Created on 2005/05/13
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.audiotools.spectrogram;

import jp.sourceforge.akjrcp.ui.draw2d.CachedImageLoader;
import jp.sourceforge.akjrcp.ui.draw2d.ScalableImageFigure;

import org.eclipse.draw2d.Graphics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;

/**
 * 
 *
 */
public class SpectrogramFigure extends ScalableImageFigure{

   
    
    private SpectrogramDrawInfo spectrogramDrawInfo;
    private Rectangle lastRectangle;
    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     */
    public SpectrogramFigure(SpectrogramDrawInfo info,CachedImageLoader arg0, String arg1, int arg2, int arg3) {
        super(arg0, arg1, arg2, arg3);
        this.spectrogramDrawInfo=info;
        
    }
    

    public double calculateImageWidth(){
    	if(lastRectangle==null){
    		Image img=getImage();
    		if(img==null){
    			return defaultWidth;
    		}
    		lastRectangle=img.getBounds();//for cache.	
    	}
    	return wzoom*lastRectangle.width;
    	
    }
    public void paintFigure(Graphics g) {
    	//log.infopaint:"+this.getClientArea());
       // log.trace(getClientArea()+","+this);
        Image spectrogramImage=getImage();
       
        
        if(spectrogramImage!=null && !spectrogramImage.isDisposed()){
        	lastRectangle=spectrogramImage.getBounds();
            //change resize.
            int w=spectrogramImage.getBounds().width;
            int h=spectrogramImage.getBounds().height;
            if(w!=defaultWidth || h!=defaultHeight){
                //resize action.
                defaultWidth=w;
                defaultHeight=h;
                resize();
                getParent().revalidate();//resized.
            }
            //clientarea is width & height.
            if(spectrogramDrawInfo.isFit()){
                g.drawImage(spectrogramImage,0,0,spectrogramImage.getBounds().width,spectrogramImage.getBounds().height,getClientArea().x,getClientArea().y,this.getSize().width,this.getSize().height);
                    
            }else{
                int drawHeight=(int)(spectrogramDrawInfo.getSampleRate()*spectrogramDrawInfo.getBaseHeight()*hzoom/SpectrogramListFigure.MAX_SAMPLERATE);
               // log.info(drawHeight);
                g.drawImage(spectrogramImage,0,0,spectrogramImage.getBounds().width,spectrogramImage.getBounds().height,getClientArea().x,getClientArea().y+getClientArea().height-drawHeight,this.getSize().width,drawHeight);
            }
            
           // g.drawImage(img,0,0,w,h,getClientArea().x,getClientArea().y,this.getClientArea().width,this.getClientArea().height);
        	}else{
        		//log.warn("no image:"+getClientArea());
        	}
        }
    
    /*
     * not sure need it or not.
     * */
    /*
    public Dimension getPreferredSize(int whint,int hhint){
    	//log.info(super.getPreferredSize(whint, hhint));
    	return new Dimension((int)(defaultWidth*wzoom),(int)(defaultHeight*hzoom));
    }*/

}
